package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.AddMealErrorException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sky.constant.MessageConstant.*;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void saveCategory(SetmealDTO setmealDTO) {
        //1. 判断是否存在相同名称的套餐
        String name = setmealDTO.getName();
        Setmeal setmeal = setmealMapper.getByName(name);
        if (setmeal!=null) {
            throw new AddMealErrorException(SET_MEAL_ERROR);
        }

        setmeal=new Setmeal();
        //2. 拷贝setmeal对象
        BeanUtil.copyProperties(setmealDTO,setmeal);

        //3. 插入setmeal数据
        setmealMapper.insert(setmeal);


        //4. 插入套餐和菜品的关联数据
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!=null&&setmealDishes.size()!=0){
            //4.1 设置dishId字段
            setmealDishes.forEach(setmealDish->{
                setmealDish.setSetmealId(id);
            });
            //4.2 批量插入setmealDish数据
            setmealDishMapper.insert(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐起售、停售状态
     * @param status
     * @param id
     */
    public void startOrStop(Long status, Long id) {
        //1. 起售时判断套餐中是否包含停售的菜品
        //1.1 查询出关联的菜品
        if (status==1){
            List<SetmealDish> dishList = setmealDishMapper.getBymealId(id);
            if (dishList!=null&&dishList.size()!=0){
                dishList.forEach((dish)->{
                    Dish dish1 = dishMapper.getById(dish.getDishId());
                    //1.2 菜品未起售，抛出异常
                    if (dish1.getStatus()==0) {
                        throw new SetmealEnableFailedException(SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        //2. 修改套餐状态
        Setmeal setmeal = Setmeal.builder()
                .status(Math.toIntExact(status))
                .id(id)
                .build();
        setmealMapper.startOrStop(setmeal);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        //查询setmeal数据
        Setmeal setmeal=setmealMapper.getById(id);

        //查询setmealDish数据
        List<SetmealDish> list=setmealDishMapper.getBymealId(id);

        //封装SetmealVO数据
        SetmealVO setmealVO = new SetmealVO();
        BeanUtil.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);

        //返回数据
        return setmealVO;
    }

    /**
     * 批量删除套餐
     */
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //1. 判断套餐中是否包含起售中的套餐
        List<Setmeal> setmealList=setmealMapper.getByIds(ids);
        if (setmealList!=null&&setmealList.size()!=0){
            setmealList.forEach((setmeal -> {
                Integer status = setmeal.getStatus();
                if (status==1){
                    throw new DeletionNotAllowedException(SETMEAL_ON_SALE);
                }
            }));
        }


        ids.forEach(id->{
            //2. 删除套餐菜品关联数据
            setmealDishMapper.deleteByMealId(id);
            //3. 删除套餐数据
            setmealMapper.deleteById(id);
        });
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        //拷贝套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(setmealDTO,setmeal);

        //更新套餐信息
        setmealMapper.update(setmeal);

        //删除套餐和菜品关联数据
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteByMealId(setmealId);

        //重新插入套餐和菜品的关联数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishMapper.insert(setmealDishes);
    }
}
